<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/7/4-15:20
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */

namespace App\Http\Controllers\Admin\Project;

use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;

class ProjectController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'project'],
        'show'  => ['get', 'project/%d'],
        'store' => ['post', 'project'],
        'update'=> ['post', 'project/%d'],

        'delete'=> ['delete', 'project/%d'],
        'setCheckStatus'=> ['post', 'project/setCheckStatus/%d'],
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    /**
     * 后台-文章分类列表
     * */
    public function index(){

        //参数部分
        $search_params = $this->request->all(['keyword']);
        $page   = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'orderBy'=>'id',
            'sortedBy'=>'desc',
            'page' => $page,
            'with'=>'admin_user;region'
        ];
        //请求部分
        $ret = http_admin($this->api_url['index'])->send($params);

        //结果处理部分
        $this->httpCodeJudge($ret);
        $data = paginate($this->request, $ret['data']);
        $data['search_params'] = $search_params;
//        dd($data['list']);
        return view('Admin.project.project_list', $data);
    }
    /**
     * 后台-新增页面
     * */
    public function create(){
        return view('Admin.project.create',['type'=>'post','project'=>[],'request_url'=>'/foradmin/project']);
    }
    /**
     * 后台-查看页面
     * */
    public function show(){

    }
    /**
     * 后台-编辑页面
     * */
    public function edit($id){
        $res = http_admin($this->api_url['show'], [$id])->send();
//        dd($res['data']['data']);
        old_set($res['data']['data']);

        return view('Admin.project.create',['type'=>'PUT','request_url'=>'/foradmin/project/'.$id]);
    }
    /**
     * 后台-新增
     * */
    public function store(){
        $data = $this->form();

        //$data = $this->request->except('_token','_method');
        old_set($data);
        $data['admin_id'] = session('admin_user_id');
//        dd($data);
        $ret = http_admin($this->api_url['store'])->send($data);
        return redirect('/foradmin/project');
    }
    /**
     * 后台-编辑
     * */
    public function update($id){

        $data = $this->form();

        //$data = $this->request->except('_token','_method');
        $data['admin_id'] = session('admin_user_id');
//        dd($data);
        $ret = http_admin($this->api_url['update'],[$id])->send($data);
        return redirect('/foradmin/project');
    }
    /**
     * 后台-删除
     * */
    public function destroy($id){
        $ret = http_admin($this->api_url['delete'],[$id])->send();
        return redirect('/foradmin/project');
    }
    /**
     * 后台-是否显示
     * */
    public function setCheckStatus($id){

        $this->validate($this->request, [
            'check_status' => 'required'
        ],[
            'check_status' => '请选择修改的状态'
        ]);

        $data['check_status'] = $this->request->get('check_status','uncheck');
        $ret = http_admin($this->api_url['setCheckStatus'],[$id])->send($data);
        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }

    /**
     * 后台-验证分类保存
     * */
    public function form(){
        $id = $this->request->get('id',false);
        $validate = [
            'type'  => 'required', // 项目类型必选
//            'announcement_type'  => 'required', // 公告类型必选
            'purchase_mode'  => 'required', // 采购方式必选
            'purchase_type'  => 'required', // 采购类型必选
            'city_id'  => 'required', // 城市必选
            'project_owner'  => 'required', // 业主单位必填
            'contacts'  => 'required', // 联系人必填
            'content'  => 'required', //项目信息内容必填
            'phone'  => 'required|regex:/[0-9\-]$/|min:8', // 联系电话必填
//            'project_type'  => 'required', //项目信息内容必填
            'check_status'  => 'required', //项目信息内容必填

            //'title' => 'required|unique:projects', // 标题
        ];
        // dd($id);
        if ($id!=false) {
            $validate['title'] = 'required|unique:projects,title,'.$id; // 标题
        }else{
            $validate['title'] = 'required|unique:projects,title'; // 标题
        }
        //dd($validate);
        $this->validate($this->request,$validate,[
            'type.required'  => '项目类型必选',
//            'announcement_type'  => '公告类型必选',
            'purchase_mode.required'  => '采购方式必选', // 采购方式必选
            'purchase_type.required'  => '采购类型必选', // 采购类型必选
            'city_id.required'  => '城市必选', // 城市必选
            'project_owner.required'  => '业主单位必填', // 业主单位必填
            'contacts.required'  => '联系人必填', // 联系人必填
            'phone.required'  => '联系电话必填', // 联系电话必填
            'phone.regex'  => '联系电话格式不正确,只能包含 特殊字符 - 和 数字', // 联系电话必填
            'phone.min'  => '联系电话最小长度为 8 ', // 联系电话必填
            'content.required'  => '项目信息内容必填', //项目信息内容必填
//            'project_type'  => '审核状态必填', //项目信息内容必填
            'check_status.required'  => '审核状态必选', //项目信息内容必填
            'title.required' => '标题名称必填',
            'title.unique' => '标题名称不可重复',
        ]);

        return $this->request->all(['id','type','announcement_type','purchase_mode','purchase_type','city_id','project_owner','contacts','phone','content','check_status','title','project_type']);
    }
}